#!/bin/bash

ARGS=("$@")

if [ ${#ARGS[@]} -ne 13 ]
then
    echo "Error: incorrect number of arguments."
    echo "Usage: test.sh config_file output_dir ds_file ds_tree_format ds_file_format pb_file pb_tree_format pb_file_format ps_file ps_tree_format ps_file_format unary_rule_file binary_rule_file"
    exit
fi

script_dir=$(dirname $0)
dist=$script_dir/../bin
pkg=edu.washington.ling.syntric.proc
jar_dir=$script_dir/../jars

config_file=${ARGS[0]}
output_dir=${ARGS[1]}
ds_file=${ARGS[2]}
ds_tree_format=${ARGS[3]}
ds_file_format=${ARGS[4]}
pb_file=${ARGS[5]}
pb_tree_format=${ARGS[6]}
pb_file_format=${ARGS[7]}
ps_file=${ARGS[8]}
ps_tree_format=${ARGS[9]}
ps_file_format=${ARGS[10]}
unary_rule_file=${ARGS[11]}
binary_rule_file=${ARGS[12]}

# Set up output directory
if [ ! -d $output_dir ]; then
	if ! mkdir $output_dir ; then
		echo Error: output_dir=\'$output_dir\' was not valid
		echo Please specify a valid output directory
		exit
	fi
fi


echo %%
echo %% 00 Creating Example File From Input Tree Files 
echo %%
if [ -d $output_dir/00-examples ]; then
	rm -rf $output_dir/00-examples
fi
mkdir $output_dir/00-examples
java -cp $dist:$jar_dir/* $pkg.CreateUnifiedExamples --output-file $output_dir/00-examples/examples.xml --output-file-format xml --ds-file $ds_file --ds-file-format $ds_file_format --ds-tree-format $ds_tree_format --pb-file $pb_file --pb-file-format $pb_file_format --pb-tree-format $pb_tree_format --ps-file $ps_file --ps-file-format $ps_file_format --ps-tree-format $ps_tree_format 2> $output_dir/00-examples/errors.txt

echo 
echo %%
echo %% 10 Checking DS Format
echo %%
if [ -d $output_dir/10-check-ds ]; then
	rm -rf $output_dir/10-check-ds
fi
mkdir $output_dir/10-check-ds
java -cp $dist:$jar_dir/* $pkg.CheckDSFormat $output_dir/00-examples/examples.xml $output_dir/10-check-ds/examples.xml --tree-format $ds_tree_format 2> $output_dir/10-check-ds/errors.txt

echo 
echo %%
echo %% 11 Checking PB Format
echo %%
if [ -d $output_dir/11-check-pb ]; then
	rm -rf $output_dir/11-check-pb
fi
mkdir $output_dir/11-check-pb
java -cp $dist:$jar_dir/* $pkg.CheckPBFormat $output_dir/10-check-ds/examples.xml $output_dir/11-check-pb/examples.xml --tree-format $pb_tree_format 2> $output_dir/11-check-pb/errors.txt

echo 
echo %%
echo %% 12 Checking PS Format
echo %%
if [ -d $output_dir/12-check-ps ]; then
	rm -rf $output_dir/12-check-ps
fi
mkdir $output_dir/12-check-ps
java -cp $dist:$jar_dir/* $pkg.CheckPSFormat $output_dir/11-check-pb/examples.xml $output_dir/12-check-ps/examples.xml --tree-format $ps_tree_format 2> $output_dir/12-check-ps/errors.txt

echo 
echo %%
echo %% 20 Merging DS and PB to create DS+PB
echo %%
if [ -d $output_dir/20-merge-ds-pb ]; then
	rm -rf $output_dir/20-merge-ds-pb
fi
mkdir $output_dir/20-merge-ds-pb
java -cp $dist:$jar_dir/* $pkg.MergeDSandPB $output_dir/12-check-ps/examples.xml $output_dir/20-merge-ds-pb/examples.xml --ds-tree-format $ds_tree_format --pb-tree-format $pb_tree_format 2> $output_dir/20-merge-ds-pb/errors.txt

echo 
echo %%
echo %% 30 Applying Consistency Rules to Create DS_Const
echo %%
if [ -d $output_dir/30-apply-const-rules ]; then
	rm -rf $output_dir/30-apply-const-rules
fi
mkdir $output_dir/30-apply-const-rules
java -cp $dist:$jar_dir/* $pkg.ApplyConsistencyRules $output_dir/20-merge-ds-pb/examples.xml $output_dir/30-apply-const-rules/examples.xml --ds-tree-format $ds_tree_format 2> $output_dir/30-apply-const-rules/errors.txt

echo 
echo %%
echo %% 40 Checking Consistency of DS_Const and PS
echo %%
if [ -d $output_dir/40-check-const ]; then
	rm -rf $output_dir/40-check-const
fi
mkdir $output_dir/40-check-const
java -cp $dist:$jar_dir/* $pkg.CheckConsistency $output_dir/30-apply-const-rules/examples.xml $output_dir/40-check-const/examples.xml --ds-tree-format $ds_tree_format --ps-tree-format $ps_tree_format 2> $output_dir/40-check-const/errors.txt


echo %%
echo %% Applying rules to get PS trees
echo %%
if [ -d $output_dir/80-apply-rules ]; then
	rm -rf $output_dir/80-apply-rules
fi
mkdir $output_dir/80-apply-rules
java -cp $dist:$jar_dir/* $pkg.ApplyRules $output_dir/40-check-const/examples.xml $output_dir/80-apply-rules/examples.xml --ds-tree-format $ds_tree_format --ps-tree-format $ps_tree_format --unary-rules $unary_rule_file --binary-rules $binary_rule_file --config config.xml 2> $output_dir/80-apply-rules/errors.txt

